<template>
  <div class="visitor-toVisit">
    <div class="search-group">
      <div class="input-wrap">
        <div class="label">姓名:</div>
        <div class="input-item">
          <j-input :maxLength="30" clearable v-model="queryParam.personName" placeholder="请输入姓名" />
        </div>
      </div>

      <div class="input-wrap">
        <div class="label">性别:</div>
        <div class="input-item">
          <a-select ref="select" allowClear v-model="queryParam.sex" placeholder="请选择性别">
            <a-select-option value="1">男</a-select-option>
            <a-select-option value="2">女</a-select-option>
          </a-select>
        </div>
      </div>

      <div class="input-wrap">
        <div class="label">身份证号:</div>
        <div class="input-item">
          <j-input :maxLength="30" clearable v-model="queryParam.idCard" placeholder="请输入身份证号" />
        </div>
      </div>

      <div class="input-wrap">
        <div class="label">人员类型:</div>
        <div class="input-item">
          <a-select ref="select" allowClear v-model="queryParam.bizType" placeholder="请选择人员类型">
            <a-select-option :value="item.value" v-for="(item, index) in temporary_person_type" :key="index">{{
              item.title
            }}</a-select-option>
          </a-select>
        </div>
      </div>

      <div class="input-wrap">
        <div class="label">核实/添加时间:</div>
        <div class="time-item">
          <start-end-time
            :times="times"
            :showTime="{ format: 'HH:mm:ss' }"
            format="YYYY-MM-DD HH:mm:ss"
            @timeChange="timeChange"
            :resetTime="resetTime"
          ></start-end-time>
        </div>
      </div>

      <div class="btn-wrap">
        <a-button
          class="searchBtn"
          @click="searchQuery(queryParam.markTime_begin, queryParam.markTime_end)"
          type="primary"
        >
          搜索
        </a-button>
        <a-button class="resetBtn" @click="searchReset()" type="primary"> 重置 </a-button>
      </div>
    </div>

    <div>
      <a-table
        rowKey="id"
        class="tableList"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        @change="handleTableChange"
      >
        <template #sex="record">
          <span>{{ record.sex == 1 ? '男' : '女' }}</span>
        </template>
        <template #birth="record">
          <span>{{ get_age(record.birth) }}</span>
        </template>
        <template #bizType="record">
          <span>{{ get_dict_type(temporary_person_type, record.bizType) }}</span>
        </template>
        <template v-slot:action="records">
          <span class="operation deatil-color" @click="detailHandle(records)">详情 </span>
        </template>
      </a-table>
    </div>
    <detail-modal ref="detailModal"></detail-modal>
  </div>
</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JInput from '@/components/jeecg/JInput'
import { mapState } from 'vuex'
import { ajaxGetDictItems } from '@/api/api'
import detailModal from '../modules/detailModal'
import startEndTime from '@/components/startEndTime'
export default {
  name: 'visitor-toVisit',
  components: { JInput, detailModal, startEndTime },
  mixins: [JeecgListMixin],
  computed: {
    ...mapState({
      isPoliceAgencyId: (state) => state.user.isPoliceAgencyId,
    }),
  },
  watch: {
    isPoliceAgencyId(v) {
      this.queryParam.policeAgencyId = v.id ? v.id : ''
      this.searchQuery()
    },
  },
  data() {
    return {
      url: {
        list: '/sys/temporary_person/page?status=2',
        tree: true,
      },
      temporary_person_type: [],
      isorter: {
        column: 'markTime',
        order: 'desc',
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 100,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '姓名',
          dataIndex: 'personName',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '性别',
          scopedSlots: { customRender: 'sex' },
          // dataIndex: 'sex',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '年龄',
          // dataIndex: 'birth',
          scopedSlots: { customRender: 'birth' },
          ellipsis: true,
          align: 'center',
        },
        {
          title: '身份证号',
          dataIndex: 'idCard',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '人员类型',
          // dataIndex: 'bizType',
          scopedSlots: { customRender: 'bizType' },
          ellipsis: true,
          align: 'center',
        },
        {
          title: '核实/添加民警',
          dataIndex: 'markPoliceName',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '核实/添加时间',
          dataIndex: 'markTime',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'action' },
          align: 'center',
        },
      ],
    }
  },
  mounted() {
    this.get_dict()
  },

  methods: {
    timeChange(v, index) {
      if (index == 1) {
        this.queryParam.markTime_begin = this.$moment(v).valueOf()
      } else {
        this.queryParam.markTime_end = this.$moment(v).valueOf()
      }
      if (v == null) {
        this.queryParam.markTime_begin = null
        this.queryParam.markTime_end = null
      }
    },

    async get_dict() {
      let { result: temporary_person_type } = await ajaxGetDictItems({ code: 'temporary_person_type' })
      this.temporary_person_type = temporary_person_type
    },

    get_age(target) {
      const specifiedDate = new Date(target)
      const currentDate = new Date()
      const yearDifference = currentDate.getFullYear() - specifiedDate.getFullYear()
      return yearDifference
    },

    get_dict_type(list, type) {
      let res = list.find((item) => item.value == type)
      if (res && res.title) {
        return res.title
      } else {
        return '暂无'
      }
    },

    detailHandle(records) {
      this.$refs.detailModal.show(records)
    },
  },
}
</script>
